package com.cloud.wms.tools;

import com.annimon.stream.Stream;
import com.cloud.wms.ui.main.entity.MemberEntity;
import com.cloud.wms.ui.main.entity.TitleEntity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Function;

/**
 * @Author:Chen
 * @Date: 2021-01-20 22:03
 * @Description:
 */
public class MemberFun<T extends MemberEntity> implements Function<ArrayList<T>, ArrayList<TitleEntity<T>>> {

    @Override
    public ArrayList<TitleEntity<T>> apply(ArrayList<T> ts) throws Exception {
        ArrayList<TitleEntity<T>> list = new ArrayList<>();
        Stream.ofNullable(ts)
                .groupBy(T::getMemberID)
                .forEach(entry -> {
                    TitleEntity<T> memberEntity = new TitleEntity<>();
                    memberEntity.setId(entry.getKey());

                    List<T> contentList = new ArrayList<>();
                    Stream.ofNullable(entry.getValue())
                            .forEach(remoteEntity -> {
                                memberEntity.setTitle(remoteEntity.getMemberName() + "");
                                contentList.add(remoteEntity);
                            });
                    memberEntity.setChildList(contentList);
                    list.add(memberEntity);
                });
        return list;
    }
}
